GSP-023 Deploying a Python Flask Web Application to App Engine Flexible

2017 10 29일 일요일

오전 5:29

링크: https://google.qwiklabs.com/focuses/6029

 

요약

Python Flask web application

App Engine Flexible environment deploy 해보자

 

사진을 업로드하고, 사진속 인물이 행복한지 분석하는 앱이다

Google Cloud API를 사용한다

- Vision, Storage, Datastore

 

About App Engine

 

create, manage, scale 이 쉽다

관리할 서버가 없다 = Serverless

 

할 수 있는 일 -  이 모든 걸 customizing 가능하다

1) Auto scaling 들어오는 트래픽에 따라

2) Load balancing

3) Microservices

4) SQL, NoSQL database

5) Traffic splitting

6) Logging

7) Searching

8) Versioning

9) Roll out / Roll back

10) security scanning

 

App Engine Environment

1) Flexible Environment: 다양하게 만질 수 있다

2) Standard Environment: 표준 환경들을 제공한다

 

배울 내용은

 

1) App Engine Flexible Environment 에 씸플한 Web App deploy 해보자

2) Google Cloud client libraries 에 접근하는 법 - Vision, Storage, Datastore

3) Cloud Shell 사용법

 

Get the sample code

 

깃헙에서 코드 가져오기

git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git

cd python-docs-samples/codelabs/flex_and_vision

 

 

Authenticate API Requests

 

Vision API enable 하기

- Datastore API, Storage API는 자동으로 enable 되어 있음

gcloud service-management list >> service.txt // 기존에 제공되던 API 확인

gcloud service-management enable vision.googleapis.com // Vision API enable

gcloud service-management list >> service2.txt  // 이후 추가된 API 확인

 

* 이런 명령들은 현재 set 되어있는 project 에 대한 값을 보여준다.

- gcloud config list project 에 나오는 그 project

 

bigquery- json . googleapis . com 
2 clouddebugger . googleapis . com 
3 cloudresourcemanager . googleapis . com 
datastore. googleapis . com 
storage - .googleapis.com 
pubsub . googleapis . com 
container. googleapis . com 
storage- api . googleapis . com 
Iogging. googleapis . com 
e resourceviews.googleapis.com 
II replicapool.googleapis.com 
2 cloudapis .googleapis . com 
sourcerepo. googleapis . 
deploymentmanager.googleapis.com 
5 containerregistry.googleapis.com 
monitoring. googleapis . 
17 dns.googleapis 
stackdriverprovisioning.googleapis.com 
compute . googleapis . com 
e sql-component.googleapis.com 
I cloudtrace. googleapis 
servicema nagement.googleapis.com 
23 replicapoolupdater.googleapis.com 
TITLE 
BigQuery API 
Stackdriver Debugger API 
Google Cloud Resource Manager API 
Google Cloud Datastore API 
Google Cloud Storage 
Google Cloud Pub/Sub API 
Google Container Engine API 
Google Cloud Storage )SON API 
Stackdriver Logging API 
Google Engine Instance Groups API 
Google Engine Instance Group Manager API 
Google Cloud APIs 
Cloud Source Repositories API 
Google Cloud Deployment Manager V2 API 
Google Container Registry API 
Stackdriver Monitoring API 
Google cloud DAS API 
Stackdriver Provisioning Service 
Google Engine API 
Google Cloud SQL 
Stackdriver Trace API 
Google Service Management API 
Google Engine Instance Group Updater API

 

bigquery -j son . googleapi s . cm 
2 clouddebugger.googleapis.com 
datastore . googleapis . com 
storage - component . googleapis . com 
pubsub . googleapis . com 
TITLE 
BigQuery API 
Stackdriver Debugger API 
r I Man 
Google Cloud Datastore API 
Google Cloud Storage 
Google Cloud Pub/Sub API 
ntainer . googleao 
Enein 
8 vision.googleapis.com 
sto rage - api. googleapis . com 
Iogging.googleapis.com 
resourceviews . googleapis . cm 
replicapool. googleapis . com 
3 cloudapis . googleapis . com 
sourcerepo . googleapis . com 
dep Ioymentmanage r. googleapis . com 
containerregistry. googleapis . com 
monitoring. googleapis . com 
dns . googleapis . com 
g stackdriverprovisioning.googleapis.com 
e compute.googleapis.com 
sqI - component . googleapis . cm 
2 cloudtrace . googleapis . com 
servicemanagement . googleapis . com 
4 replicapoolupdater. googleapis . ca' 
Google Cloud Vision API 
Google Cloud Storage JSN' API 
Stackdriver Logging API 
Google Compute Engine Instance Groups API 
Google Compute Engine Instance Group Manager API 
Google Cloud APIs 
Cloud Source Repositories API 
Google Cloud Deployment Manager V2 API 
Google Container Registry API 
Stackdriver Monitoring API 
Google cloud DNS API 
Stackdriver Provisioning Service 
Google Compute Engine API 
Google Cloud SQL 
Stackdriver Trace API 
Google Service Management API 
Google Compute Engine Instance Group Updater API

 

 

export 해주기

- 많이 쓰이는 값들을 환경변수로 설정해주자

export PROJECT_ID=[YOUR_PROJECT_ID]  // 현재 내 project id

export GOOGLE_APPLICATION_CREDENTIALS="/home/${USER}/key.json" // echo $USER 해보면 넣을 값을 알 수 있겠지?

 

Local 에서 Google Cloud APIs access 있는 Service Accout 만들자

 

gcloud iam service-accounts create codelab \ 
--display-name "My Codelab Service Account"

 

codelab 이라는 account 를 만든다

gcloud projects add-lam-policy-binding PROJECT _ ID 
--member serviceAccount : codelab@- PROJECT_ID . iam.gserviceaccount.com \ 
-role roles 'owner

 

Google Cloud Platform 
IAM & admin 
IAM 
Identity 
Quotas 
Service accounts 
Labels 
GCP Privacy & Security 
Settings 
Encryption keys 
Identity-Aware Proxy 
My Project 
IAM 
ADD 
-2 REMOVE 
Permissions for project "My Project" 
These permissions affect the entire 'My Project' project and all of its resources. 
To grant permissions, add a member and then select a role for thenm Members 
can be people, domains, groups, or service accounts 
Some roles are in beta development and might be changed or deprecated in the 
future. Learn more [_4 
Filter by name or role 
View by: Members 
- 
Type 
Members A 
742008186345-compute@developer_gserviceaccountcom 
codelab@polished-zephyr-176321 _ iam gserviceaccountcom 
Role(s) 
Editor 
Editor 
Owner

 

codelab 계정을

- 내 프로젝트에 바인딩하고

- serviceAccount 멤버로 지정하고

- 마지막으로 role owner로 지정해준다

gcloud iam service-accounts keys create N/key. json \ 
--iam-account codelab@- PROJECT_ID . iam.gserviceaccount.com

 

key.json은 대충 이렇게 생겨먹음

 

"type" • 
project 
id": " 
private_key_id" 
private_key" 
"client email" 
"client id" 
"auth uri 
"éttps 
"token uri 
row 
"auth_provider x509_cert urI" 
"client x509 cert url": 
"h . zephy' 
: onl

 

그리고 key를 생성해준다

 

Testing the Application Locally

 

내 컴퓨터가 지저분해지지 않게 가상환경에서 처리해보자

 

 

 

가상환경 나오려면

deactivate 치면 됨

 

gunicorn=19.7. I 
google -Cloud- 27 . 
google -cloud- 5 . 
google -cloud- datastore=l . 3 . O

 

 

 

참고. 가상환경을 activate 하건 안하건 파일들을 접근하고 만질수는 있다. 하지만 requrements.txt 설치한건 안먹힌다

and_vision$ python main. py 
Traceback (most recent call last) : 
File "main.py", line 2e, in 
from flask import Flask, redirect, render_template, request 
ImportError: module nard flask 
and_vision$ source env/bin/activate 
(env) and_vision$ python main.py 
Running on http://127.ø.ø.I:808ø/ (Press CTRL+C to quit) 
Restarting with stat 
* Debugger is active! 
* Debugger PIN: 219-676-919

 

 

 

Storage Bucket을 만들어보자

 

 

스토리지 이름은 권장사항임

 

 

 

 

beba9defc782eee6.png

 

 

 

코드 보기

 

샘플 프로젝트 구조는 이렇다

templates/ 
homepage. html 
app. yam1 
main. py 
requi rements. txt 
HTML template that uses Jinja2 
App Engine application configuration file 
Python Flask web application 
List of dependencies for the project

 

main.py

Flask web application 이다

- 사용자가 사진을 등록하면Cloud Storage에 저장하고

- Cloud Vision API로 얼굴인식후 분석한다음

- Key information Datastore (= GCP NoSQL 데이터베이스)에 저장된다

- 사용자는 Datastore에 매번 접근한다

 

구글 클라우드 API 가져오기

from google. cloud import datastore 
from google. cloud import storage 
from google. cloud Import vision

 

홈페이지 방문하면 실행되는 것

- datasore_client를 생성하고

- kind='Faces' 로 쿼리를 설정한다음

- query.fetch()로 가져온 진짜 값을 list로 저장함 to image_entities

- render_template를 통해 homepage.html 에 있는 image_entities에 값을 전송해준다.

35 Capp . route( 
34 def homepage() 
Create s Cloud Datastore client. 
datastore_client = datastore . Client() 
use the Cloud Datastore client to fetch information from Datastore about 
each photo. 
query = datastore_client . 
image entities 
list (query .fetch()) 
Return Jin:s2 HTML template and pass in image entities as s parameter 
render_template ( 
image entities—image entities) 
return

 

이제 entities Datastore에 저장되는 부분을 보자

- Datastore entities 라 부르는 object를 저장한다

# Create a Cloud Datastore client. 
datastore_client = datastore. Client() 
# Fetch the current date / time. 
current_datetime = datetime. now() 
# The kind for the new entity. 
kind = 
' Faces 
# The name/ID for the new entity. 
name = blob .name 
# Create the Cloud Datastore key for the new entity. 
key = datastore_client. key(kind, name) 
# Construct the new entity using the key. Set dictionary values for entity 
# keys blob_name, storage_public_url, timestamp, and joy. 
entity = datastore. Entity(key) 
entity[ ' blob_name' ] 
= blob. name 
entity[ = blob . public_url 
entity[ ' timestamp ' ] 
— current_datetime 
entity[' joy'] = face _ joy 
# Save the new entity to Datastore. 
datastore_client. put(entity)

 

homepage.html

- Flash web framework Jinja2 라는 템플릿 엔진을 사용한다

- main.py 의 변수와 표현을 homepage.html로 전달해주는 역할을 한다.

- 전달된 내용은 페이지가 랜더링되면서 반영된다.

 

•Google Cloud Platform - Face Detection Sample 
•This Python Flask application demonstrates App Engine Flexible, 
Storage, Datastore, and the Cloud Vision API. 
Google Cloud 
efDrm method:" POST" 
Upload File: 
type:" file" 
•:br-s 
type:" submit" 
for image_entity in image_entities 
src=" width=2ee height=2BW 
blob_name• l}} was uploaded 'timestamp'] } 
•Joy Likelihood for Face: joy' ] } } 
endfor

 

만든 앱엔진을 deploy 해보자

 

app.yaml을 만지면 된다

runtime: python 
env: flex 
entrypoint: gunicorn -b 
runtime_config : 
python_version: 3 
env _variables : 
:SPORT main :app 
<your -cloud-storage-bucket>

 

아까 만들땐 gcloud app create 였고 이번엔

 

이제 들어가보자

https://< PROJECT_ID >.appspot.com

 

Microsoft OneNote 2016에서 작성